package com.ciaojian.core.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ciaojian.core.constant.RedisConstant;
import com.ciaojian.core.enums.business.OrderIsUserMemberEnum;
import com.ciaojian.core.mapper.OrderCommissionMapper;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.OrderComment;
import com.ciaojian.core.model.OrderCommission;
import com.ciaojian.core.model.WorkerService;
import com.ciaojian.core.util.LogUtil;
import com.ciaojian.core.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import sun.security.jca.ServiceId;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author oxiny
 */
public abstract class OrderCommissionService extends ServiceImpl<OrderCommissionMapper, OrderCommission> {
    @Autowired
    private WorkerServiceService workerServiceService;
    @Resource
    private OrderService<OrderMapper, Order> orderService;

    /**
     * 评分后新增提成记录
     *
     * @param orderComment 订单评价
     */
    public void insertAfterComment(OrderComment orderComment) {
        // 是否统计该评分
        if (orderComment.getIsSum()) {
            if (orderComment.getScore().compareTo(1) == 0) {
                new OrderCommission()
                        .setWorkerId(orderComment.getWorkerId())
                        .setOrderId(orderComment.getOrderId())
                        .setType(1)
                        .setAmount(-5000)
                        .setRemark("差评")
                        .insert();
            }
        }
    }

    /**
     * 剪发新增提成记录
     *
     * @param order 订单
     */
    public void insert(Order order) {
        // 当用户取号19.9服务时，按发型师剪发原价计算
        String newer = "新人特惠";
        // 提成数额
        int amount;
        // 当时的服务价格
        int servicePrice = 0;

        int serviceId = 5;
        if (order.getType().contains("儿童剪")){
            serviceId = 12;
        }

        if (order.getType() != null && order.getType().contains(newer)) {

            // 获取发型师剪发原价
            WorkerService workerService = Optional.ofNullable(workerServiceService.lambdaQuery()
                            .eq(WorkerService::getServiceId, serviceId)
                            .eq(WorkerService::getWorkerId, order.getWorkerId())
                            .one())
                    .orElseThrow(() -> new IllegalArgumentException("未获取到发型师的剪发原价格"));
            LogUtil.writeBusinessLog(String.format("发型师:%s服务价格:%s", workerService.getWorkerId(), workerService.getPrice()));
//            amount = workerService.getPrice();
            amount = workerService.getCommission();
            servicePrice = amount;
        } else {
            // 获取发型师剪发原价
            WorkerService workerService = Optional.ofNullable(workerServiceService.lambdaQuery()
                    .eq(WorkerService::getServiceId, serviceId)
                    .eq(WorkerService::getWorkerId, order.getWorkerId())
                    .one())
                    .orElseThrow(() -> new IllegalArgumentException("未获取到发型师的剪发原价格"));
            amount = workerService.getCommission();
            servicePrice = amount;

//            amount = order.getGoodsTotal().intValue() * 100;
        }

        new OrderCommission()
                .setWorkerId(order.getWorkerId())
                .setOrderId(order.getId())
                .setAmount(amount)
                .setRemark(order.getType().concat("此时的服务价格是:").concat(String.valueOf(servicePrice)))
                .setType(0)
                .insert();
    }

    /**
     * 购买会员新增提成记录
     *
     * @param order 订单
     */
    public void insertIfMemberDetail(Order order) {
        Optional<Order> optional = Optional.of(order);
        optional.map(Order::getInviterId) //getInviterId邀请发型师
                // 订单得有绑定发型师ID才能为发型师记录提成
                .ifPresent(inviterId -> {
                    // 新用户购买会员每单10元提成/老用户购买会员每单5元
                    Integer count = orderService.countCompletedHairCutByUser(order.getUserId());
                    // 会员提成
                    new OrderCommission()
                            .setWorkerId(inviterId)
                            .setOrderId(order.getId())
                            .setType(2)
//                            .setAmount(order.getCustomerType() == 3 ? 1000 : 500)
//                            .setAmount(Integer.valueOf((count.intValue() == 0) ? 1000 : 500))
                            .setAmount(order.getIsUserMember() == OrderIsUserMemberEnum.NON_MEMBER ? 1000 : 500)
                            .setRemark("会员提成")
                            .insert();

                    // 会员业绩数额
                    int amount = order.getGoodsTotal().intValue() * 100;
                    int type = 3;
                    String remark = "";
                    // 当用户使用抖音券剪发，后购买会员时，此单会员业绩按两倍计算
                    if (RedisUtil.exists(RedisConstant.USED_DOUYIN_ORDER + order.getUserId())) {
                        amount *= 2;
                        remark = remark.concat(",使用抖音券剪发双倍业绩");
                        type = 4;
                    }
                    // 抖音会员业绩
                    new OrderCommission()
                            .setWorkerId(inviterId)
                            .setOrderId(order.getId())
                            .setType(type)
                            .setAmount(amount)
                            .setRemark("会员业绩".concat(remark))
                            .insert();
                });
    }

}
